<template>
	<view class="">
		<dsl-navbar title="发布公告" isBack bgTopImg="white"></dsl-navbar>
		<view class="">
			<view class="padding solid-bottom bg-white">
				<input type="text" value="" placeholder="请输入公告标题" v-model="title" maxlength="9" />
			</view>
			<view class="padding bg-white">
				<view>公告内容</view>
				<textarea type="text" value="" placeholder="请输入公告内容" class="margin-top-sm" v-model="content" />
			</view>
		
			<view class="bg-white padding margin-top-sm rowsb" @click="selectMen()">
				<!-- <picker @change="PickerChange" :value="index" :range="tenterpriseList" range-key="name">
					<view class="picker">
						{{index>-1?tenterpriseList[index].name:'请选择接收人'}}
					</view>
				</picker>
				<text class="cuIcon-right"></text> -->
				<view class="">
					选择接收人
				</view>
				<view class="cuIcon-right"></view>
			</view>
			<view v-for="(item,index) in iseList" :key="index" class="padding solid-bottom">
				{{item.username?item.username:item.name}}
			</view>
			<view class="bg-white padding margin-top-sm rowsb align-center"  @click.stop="isF =!isF">
				<!-- <text  @click="updataImgList" class="button">选择封面图片</text> -->
				<text >选择封面图片</text>
				<!-- @click="updataImgList" -->
				<text class="cuIcon-unfold"></text>
			</view>
		 
			<view class=" padding bg-white " v-if="isF">
				<view v-for="(item,index) in announcement_images"@click="num = index"  class="margin-top-xs">
					<image :src="item" mode="aspectFill" class="radius-sm" style="height: 300rpx;width: 100%;" :class="{'active_solid':index==num}"></image>
				</view>
			</view>
			<view class="submit" @click="submit">
				发布
			</view>
		</view>
	</view>
</template>

<script>
	const app = getApp().globalData
	import {
		updataImgList
	} from '@/common/updataImgList.js'
	import {
		messageAdd, //f发布
		getenterprise, //用工企业人员
		messageEdit, //公告修改
		getsystem, //基础配置
	} from '@/common/path.js'
	export default {
		data() {
			return {
				isF:false,
				title: '',
				content: '',
				image: '',
				recipient: '', //接收人id
				tenterpriseList: [], //选择接受人
				index: -1, //默认不选
				id: '', //接收人id
				detailId: '', //编辑id
				issueEdit: 0, //0=发布 ，1=编辑
				iseList: [], //选择的联系人
				type: '', //联系人type
				announcement_images: [], //基础配置
				num:0 ,//默认选中的图片
			}
		},
		watch:{
			num(i){
				this.image = this.announcement_images[i]
			}
		},
		//生命周期函数
		onReady() {},
	
		onLoad(option) {
			if (option.obj) {
				this.issueEdit = 1
				let obj = JSON.parse(option.obj)
				console.log('[obj]', obj);
				this.title = obj.title
				this.content = obj.content
				this.image = obj.image
			} 
			this.getsystem()
		},
		onShow() {
			this.iseList = app.talentsList.list
			this.type = app.talentsList.type
			console.log('[触发]', app);
			this.getenterprise()
		},
		methods: {
			//基础配置
			getsystem() {
				this.Http.POST(getsystem).then(res => {
					if (res.code == 1) {
						console.log('[基础配置]', res);
						this.announcement_images = res.data.announcement_images
						this.image = res.data.announcement_images[0]
	
					}
				})
			},
	
			selectMen() {
				uni.navigateTo({
					url:'/pages/enterprise/talentsSelect'
				})
			},
	
			//选择接收人
			PickerChange(e) {
				this.index = e.detail.value
				this.id = this.tenterpriseList[e.detail.value].id
			},
			getenterprise() {
				let data = {
					page: 1,
					listrows: 999
				}
				this.Http.POST(getenterprise, data)
					.then((res) => {
						if (res.code == 1) {
							this.tenterpriseList = res.data
						}
					})
			},
	
			submit() {
				if (this.title == '') {
					this.$tools.showToast('请填写标题')
					return
				}
				if (this.content == '') {
					this.$tools.showToast('请填写公告内容')
					return
				}
				if (this.image == '') {
					this.$tools.showToast('请选择封面图片')
					return
				}
				if (this.iseList.length == 0) {
					this.$tools.showToast('请选择接收人')
					return
				}
				let list = []
				this.iseList.forEach(item => {
					list.push(item.id)
				})
	
				let data = {
					title: this.title,
					content: this.content,
					recipient: list.toString(),
					image: this.image,
					recipient_type: this.type
				}
	
				let url = messageAdd
				if (this.issueEdit == 1) {
					url = messageEdit
					data.id = this.detailId
				}
				this.Http.POST(url, data)
					.then((res) => {
						console.log('[发布]', res);
						if (res.code == 1) {
							this.$tools.showToast2(res.msg)
							setTimeout(r => {
								this.$tools.back()
							}, 1000)
						} else {
							this.$tools.showToast(res.msg)
						}
					})
			},
	
			//选择图片
			updataImgList() {
				updataImgList()
					.then(res => {
						console.log('[图片]', res);
						if (res.code == 1) {
							this.image = res.data.url
							this.$tools.showToast('成功')
						} else {
							this.$tools.showToast('网络异常')
						}
					})
			}
		},
		computed: {},
		components: {}
	}
</script>

<style>
	page {
		background: #F7F6FA;
	}

	textarea {
		width: 100%;
	}

	page {
		background: #F7F6FA;
	}
	
	textarea {
		width: 100%;
	}
	
	.submit {
		width: 654rpx;
		height: 98rpx;
		background: #00D88F;
		box-shadow: 0px 1rpx 10rpx rgba(0, 216, 143, 0.5);
		border-radius: 60rpx;
		color: white;
		text-align: center;
		line-height: 98rpx;
		margin: 0 auto;
		margin-top: 100rpx;
	}
	.active_solid{
		/* box-shadow: 0rpx 0rpx 50rpx rgba(0, 216, 143, 0.5); */
		border: 10rpx solid #00D88F;
	}
	.button{
		background-color: #00D88F;
		color: white;
		padding: 20rpx;
		border-radius: 6rpx;
		font-size: 24rpx;
	}
</style>
